Thread: deleting structs in an array and sorting the array of structs[ascending]..

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    deleting structs in an array and sorting the array of structs[ascending]..

    here is my code and i have some questions....

    1.) any suggestions how would i delete accounts in such a way that if i delete one, then that empty slot is immediately occupied by accounts on its right [if there are any]...?

    2.)i want to sort my array of struct [ascending] according to every stud's student number... where should i implement this sorting?
    a.) whenever i create an account [to find a free slot AND a slot where the accnt fits[ascending]]?
    b.) upon displaying the accounts? [i.e i sort 1st before displaying]

    and which is easier and less complicated? a or b?

    thanks....

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct student_profile{
    	long long int stud_num;
    	char name[50];
    	char bday[50];
    	char course[50];
    	} STUDENT;
    
    void login(char username[], char password[]);
    char admin(void);
    	void add_student(STUDENT array[20]);
    	void display_students(STUDENT array[20]);
    	void modify_student(STUDENT array[20]);
    	void search_student(STUDENT array[20]);
    	//void delete_student(STUDENT array[20]);
    	void delete_all(STUDENT array[20]);
    
    //*******************************************************************
    
    int main(void)
    {
    	STUDENT array[20];
    	char username[50];
    	char password[50];
    	char option;
    	int i;
    
    	for(i = 0; i < 20; i++)
    	{
    		array[i].stud_num = 0;
    	}
    
    	clrscr();
    
    	while(strcmp(username,"quit") != 0 || strcmp(password,"quit") != 0)
    	{
    
    	login(username, password);
    
    
    	if(strcmp(username, "ADMIN") == 0 && strcmp(password, "admin") == 0)
    	{
    		option = '0';
    
    		while(option != 'x')
    		{
    			option = admin();
    			switch(option)
    			{
    				case '1' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Add Student] Function --->...");
    					getche();
    					add_student(array);
    					break;
    				}				//end of case '1'
    
    				case '2' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Modify Student Account] Function --->...");
    					getche();
    					modify_student(array);
    					break;
    				}				//end of case '2'
    
    				/*case '3' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Delete Student Account] Function --->...");
    					getche();
    					delete_all(array);
    					break;
    				}*/				//end of case '3'
    
    				case '4' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Search Student Account] Function --->...");
    					getche();
    					search_student(array);
    					break;
    				}				//end of case '4'
    
    				case '5' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Display all Accounts] Function --->...");
    					getche();
    					display_students(array);
    					break;
    				}				//end of case '5'
    
    				case '6' :
    				{
    					gotoxy(14,48); printf("Redirecting to [Delete all Accounts] Function --->...");
    					getche();
    					delete_all(array);
    					break;
    				}				//end of case '6'
    
    
    				case '7' :
    				{
    					gotoxy(14,50); printf("Logging out...");
    					getche();
    					option = 'x';
    					break;
    				}				//end of case '7'
    
    				case 'x' :
    				{	gotoxy(14,48); printf("Terminating Program...");
    					getche();
    					return 0;
    				}				//end of case '8'
    
    				default :
    				{	printf("\n\nInvalid option");
    				}  				//end of default
    			}					//end of switch
    		}						//end of while(option != '6')
    	}							//end of while(strcmp...)
    
    	/*else if(ang account kay student)
    	{
    	}*/
    
    	else
    	{
    		if(strcmp(username,"quit") != 0 && strcmp(password,"quit") != 0)
    		{
    			gotoxy(19,30); printf("Invalid username or password.");
    		}
    
    	}
    	}							//end of super big loop! XD haha
    
    	gotoxy(19,31); printf("Terminating Program...");
    	getche();
    	return 0;
    }
    
    //*******************************************************************
    
    void login(char username[], char password[])
    {
    	clrscr();
    
    	gotoxy(19,5);  printf("********* ********    ***     *** *********");
    	gotoxy(19,6);  printf("**    ***  **    ***   ***   ***  **    ***");
    	gotoxy(19,7);  printf("**     **  **     ***  ***   ***  **     **");
    	gotoxy(19,8);  printf("***        **    ***   **** ****  ***      ");
    	gotoxy(19,9);  printf("********   *******     ** *** **  ******** ");
    	gotoxy(19,10); printf(" ********  **    ***   ** *** **   ********");
    	gotoxy(19,11); printf("      ***  **     **   **  *  **        ***");
    	gotoxy(19,12); printf("**     **  **     **   **     **  **     **");
    	gotoxy(19,13); printf("***    **  **     **   **     **  ***    **");
    	gotoxy(19,14); printf("********* ***     *** ****   **** *********");
    	gotoxy(45,40); printf("Just type 'quit' as username");
    	gotoxy(45,41); printf("and 'quit' also as password");
    	gotoxy(45,42); printf("to terminate program.");
    	gotoxy(45,45); printf("Student Record Management System");
    	gotoxy(19,22); printf("Enter username:");
    	gotoxy(35,22); gets(username);
    	gotoxy(19,24); printf("Enter password:");
    	gotoxy(35,24); gets(password);
    }
    
    //*******************************************************************
    
    char admin(void)
    {
    	char option;
    
    	clrscr();
    
    	gotoxy(14,2); printf("===================================================");
    	gotoxy(14,4); printf("               Administrator's Menu");
    	gotoxy(14,6); printf("===================================================");
    
    	gotoxy(14,10); printf("[1]     ---     Add a Student");
    	gotoxy(14,12); printf("[2]     ---     Modify a Student Account.");
    	gotoxy(14,14); printf("[3]     ---     Delete a Student Account.");
    	gotoxy(14,16); printf("[4]     ---     Search a Student Account.");
    	gotoxy(14,18); printf("[5]     ---     Display all Student Account Entries.");
    	gotoxy(14,20); printf("[6]     ---     Delete all Student Account Entries.");
    	gotoxy(14,22); printf("[7]     ---     Logout");
    	gotoxy(14,24); printf("[x]     ---     Terminate Program.");
    
    	gotoxy(14,28); printf("===================================================");
    
    	gotoxy(14,32); printf("Please choose the task you want to perform.");
    	gotoxy(14,34); scanf("%c", &option);
    
    	return option;
    }
    
    //*******************************************************************
    
    void add_student(STUDENT array[20])
    {
    	int i = 0;
    	int empty;
    	char opt = 'y';
    
    	while(opt == 'y' && i < 19){
    	for(i = 0; i < 20; i++)
    	{
    		if(array[i].stud_num == 0)
    		{
    			empty = i;
    			break;
    		}
    	}
    
    	clrscr();
    
    	if(i >= 19)
    	{
    		gotoxy(14,2);  printf("===================================================");
    		gotoxy(14,4);  printf("            Enter account information");
    		gotoxy(14,6);  printf("===================================================");
    		gotoxy(14,22); printf("===================================================");
    		gotoxy(14,32); printf("There are currently no free slots.");
    		gotoxy(14,26); printf("===================================================");
    
    	}
    
    	else
    	{
    		gotoxy(14,2);  printf("===================================================");
    		gotoxy(14,4);  printf("            Enter account information");
    		gotoxy(14,6);  printf("===================================================");
    		gotoxy(14,22); printf("===================================================");
    		gotoxy(14,26); printf("===================================================");
    
    		gotoxy(16,9);  printf("Student Number  [Username]:");
    		gotoxy(16,13); printf("Name:");
    		gotoxy(16,15); printf("Birthdate:");
    		gotoxy(16,17); printf("Course:");
    
    		gotoxy(32,11); scanf("%lld", &array[empty].stud_num);
    		gotoxy(32,13); gets(array[empty].name);					//try unya ang fgets(); if sakto na...
    		gotoxy(32,13); gets(array[empty].name);
    		gotoxy(32,15); gets(array[empty].bday);
    		gotoxy(32,17); gets(array[empty].course);
    
    		gotoxy(14,24); printf("       --- Account successfully created ---");
    
    		if(i >= 19)
    		{
    			clrscr();
    			gotoxy(14,2);  printf("===================================================");
    			gotoxy(14,4);  printf("            Enter account information");
    			gotoxy(14,6);  printf("===================================================");
    			gotoxy(14,22); printf("===================================================");
    			gotoxy(14,24); printf("There are currently no free slots.");
    			gotoxy(14,26); printf("===================================================");
    
    		}
    
    		else
    		{
    			gotoxy(14,32); printf("Do you want to enter another account?");
    			gotoxy(14,33); printf("[y for yes, else no]");
    			gotoxy(14,35); opt = getche();
    		}
    	}
    	} // end of while loop
    
    	gotoxy(14,48); printf("<--- Redirecting to the Administrator's page...");
    	getche();
    }
    
    //*******************************************************************
    
    void display_students(STUDENT array[20])
    {
    	int i, j;
    	long long int temp;
    
    	clrscr();
    
    	gotoxy(6,2);  printf("=====================================================================");
    	gotoxy(6,4);  printf("STUDENT NUMBER");
    	gotoxy(25,4); printf("NAME");
    	gotoxy(45,4); printf("BIRTHDATE");
    	gotoxy(65,4); printf("COURSE");
    	gotoxy(6,6);  printf("=====================================================================");
    
    	//for(i = 1; i <= 20 - 1; i++)										/*passes*/
    	//{
    	//    for(j = 0; j <=  20-2; j++)  									/*one pass*/
    	 //   {
    	//        if(array[j].stud_num > array[j + 1].stud_num)    			/*one comparison*/
    	//        {
    	//			temp = array[j].stud_num;        						/*one swap*/
    	//            array[j].stud_num = array[j + 1].stud_num;
    	//          	array[j + 1].stud_num = temp;
            //    }
    	//	}
    	//}
    
    	for(j = 10, i = 0; array[i].stud_num != 0; j++, i++)
    	{
    		if(array[i].stud_num > 0)
    		{
    			gotoxy(6,j);  printf("%lld", array[i].stud_num);
    			gotoxy(25,j); puts(array[i].name);
    			gotoxy(45,j); puts(array[i].bday);
    			gotoxy(65,j); puts(array[i].course);
    		}
    	}
    
    	gotoxy(6,j+2); printf("\=====================================================================");
    	gotoxy(6,48);  printf("<----- Redirecting to the Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************
    
    void modify_student(STUDENT array[20])
    {
    	int i;
    	long long int stud_num;
    
    	clrscr();
    	printf("\n\nPlease enter the student number of the account to be modified:\n\n");
    	scanf("%lld", &stud_num);
    	printf("\n\nSearching Student Account with Student Number %lld...", stud_num);
    
    	for(i = 0; i < 20; i++)
    	{
    		if(stud_num == array[i].stud_num)
    			break;
    	}
    
    	if(i >= 19)
    	{
    		printf("\n\nThe given student number did not match any of the existing accounts.\n");
    		printf("\n\nRedirecting to Administrator's Page...");
    		getche();
    	}
    
    	else
    	{
    		gotoxy(1,10);  printf("=====================================================");
    		gotoxy(1,12);  printf("Modifying the account of %s", array[i].name);
    		gotoxy(1,14);  printf("=====================================================");
    		gotoxy(1,16);  printf("Current Student Number: %lld", array[i].stud_num);
    		gotoxy(40,16); printf("New Student Number:");
    		scanf("%lld", &array[i].stud_num);
    		gotoxy(1,18);  printf("Current Name: %s", array[i].name);
    		gotoxy(40,18); printf("New Name: ");
    		gets(array[i].name);
    		gets(array[i].name);
    		gotoxy(1,20);  printf("Current Birthdate: %s", array[i].bday);
    		gotoxy(40,20); printf("New birthdate: ");
    		gets(array[i].bday);
    		gotoxy(1,22);  printf("Current Course: %s", array[i].course);
    		gotoxy(40,22); printf("New Course: ");
    		gets(array[i].course);
    
    		printf("\n\n=====================================================");
    		printf("\n\nStudent account modified...");
    		printf("\n\nRedirecting to Administrator's Page...");
    		printf("\n\n=====================================================");
    		getche();
    	}
    }
    
    //*******************************************************************
    
    /*void delete_student(STUDENT array[20])
    {
    	printf("\n\nEnter the ");
    
    }*/
    
    //*******************************************************************
    
    void delete_all(STUDENT array[20])
    {
    	int i;
    	char option;
    
    	clrscr();
    	gotoxy(14,2);  printf("===================================================");
    	gotoxy(14,4);  printf("       Delete All Student Accounts Function        ");
    	gotoxy(14,6);  printf("===================================================");
    	gotoxy(14,24); printf("===================================================");
    	gotoxy(14,28); printf("===================================================");
    	gotoxy(14,12); printf("Are you sure you want to delete all student accounts?");
    	gotoxy(14,14); printf("press 'y' for yes, else a no...");
    	gotoxy(20,18); option = getche();
    
    	if(option == 'y')
    	{
    		for(i = 0; i < 20; i++)
    		{
    			array[i].stud_num = 0;
    			strcpy(array[i].name, "");
    			strcpy(array[i].bday, "");
    			strcpy(array[i].course, "");
    		}
    
    		gotoxy(14,26); printf("        Deletion of All Accounts Successful        ");
    	}
    
    	else
    	{
    		gotoxy(14,26); printf("                     Aborted                       ");
    	}
    
    
    	gotoxy(14,48); printf("<-- Redirecting to Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************
    
    void search_student(STUDENT array[20])
    {
    	long long int stud_num;
    	int i;
    
    	clrscr();
    	gotoxy(14,2);  printf("===================================================");
    	gotoxy(14,4);  printf("             Search Student Function               ");
    	gotoxy(14,6);  printf("===================================================");
    	gotoxy(14,10); printf("Enter Student Number:");
    	gotoxy(40,10); scanf("%lld", &stud_num);
    	gotoxy(14,14); printf("Searching Account with Student Number %lld..", stud_num);
    
    	for(i = 0; i < 20; i++)
    	{
    		if(stud_num == array[i].stud_num)
    			break;
    	}
    
    	if(i >= 19)
    	{
    		gotoxy(14,18); printf("The given student number did not match");
    		gotoxy(14,20); printf("any of the existing accounts.");
    		getche();
    	}
    
    	else
    	{
    		gotoxy(14,18); printf("Student Number:");
    		gotoxy(35,18); printf("%lld", array[i].stud_num);
    		gotoxy(14,20); printf("Name");
    		gotoxy(35,20); printf("%s", array[i].name);
    		gotoxy(14,22); printf("Birthdate:");
    		gotoxy(35,22); printf("%s", array[i].bday);
    		gotoxy(14,24); printf("Course:");
    		gotoxy(35,24); printf("%s", array[i].course);
    	}
    
    	gotoxy(14,48); printf("<-- Redirecting to Administrator's Page...");
    	getche();
    }
    
    //*******************************************************************

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would do the sorting only when the user request a list. That way there is less messing about keeping the list sorted at all times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    ok sir thanks a lot...
    ill do my coding now...
    consultation time later... please?
    thanks =)

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Both options are fairly easy and uncomplicated, but using qsort to sort before printing would probably be least code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an Array of Structs not working
    By ashcan1979 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2006, 03:07 PM
  2. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. sorting an array of structs
    By jo_jo2222 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 11:52 PM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM